import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:safetymonitor/app/core/utils/file_util.dart';
import 'package:safetymonitor/app/core/utils/function_util.dart';
import 'package:safetymonitor/app/core/values/colors.dart';
import 'package:safetymonitor/app/core/values/dimens.dart';

/// @Author: leijingxing
/// @Date: 2023-3-17
/// @LastEditTime: 2023-3-17
/// @LastEditors: leijingxing
/// @Description: widget的扩展类,可以方便快速创建点击事件
extension WidgetExtension on Widget {
  /// 点击事件，防止快速点击
  ///
  /// [disable] 表示是否要禁用点击事件, 例如点击作者头像, 如果当前用户为作者则不响应
  /// [showHighlight] 是否显示水波纹
  /// ```dart
  /// Container(
  ///   child: Text('点击我'),
  /// ).clickOnce(() {
  ///   print('点击了我');
  /// });
  /// ```
  Widget clickOnce(
    VoidCallback onTap, {
    bool disable = false,
    bool showHighlight = false,
    VoidCallback? onLong,
    VoidCallback? onDoubleTap,
  }) {
    if (disable) {
      return this;
    }

    Widget child = InkWell(
      highlightColor: showHighlight ? null : Colors.transparent,
      splashColor: showHighlight ? null : Colors.transparent,
      // 不设置的话，在评价的页面不知道为什么会出现灰色的边框
      hoverColor: Colors.transparent,
      focusColor: Colors.transparent,
      onTap: () {
        FunctionUtil.throttle(() {
          onTap.call();
        });
      },
      onLongPress: onLong,
      onDoubleTap: onDoubleTap,
      child: this,
    );

    if (showHighlight) {
      child = Material(
        color: Colors.transparent,
        child: child,
      );
    }

    return child;
  }

  /// 点击事件
  /// [showHighlight] 是否显示水波纹
  /// ```dart
  /// Container(
  ///   child: Text('点击我'),
  /// ).click(() {
  ///   print('点击了我');
  /// });
  /// ```
  Widget click(
    VoidCallback onTap, {
    bool showHighlight = false,
    VoidCallback? onLong,
  }) {
    return InkWell(
      highlightColor: showHighlight ? null : Colors.transparent,
      splashColor: showHighlight ? null : Colors.transparent,
      onTap: onTap,
      onLongPress: onLong,
      child: this,
    );
  }

  /// 显示 BottomSheetDialog
  /// ```dart
  /// Container(
  ///  child: Text('点击显示BottomSheetDialog'),
  /// ).showBottomSheetDialog();
  /// ```
  Future<T?> showBottomSheetDialog<T>({bool isDismissible = true}) async {
    return Get.bottomSheet<T>(
      this,
      backgroundColor: Colors.white,
      isDismissible: isDismissible,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(AppDimens.dp12),
        ),
      ),
    );
  }

  /// 将整个Widget灰度化,
  /// ```dart
  /// Container(
  ///  child: Text('灰度化'),
  /// ).gray();
  /// ```
  Widget gray({bool gray = true}) {
    if (!gray) {
      return this;
    }
    return ColorFiltered(
      colorFilter: AppColorFilters.gray,
      child: this,
    );
  }

  /// 点击显示大图
  /// ```dart
  /// Image.network('https://scpic.chinaz.net/files/pic/pic9/202207/apic42382.jpg').clickShowLarge(
  ///   url: 'https://scpic.chinaz.net/files/pic/pic9/202207/apic42382.jpg',
  /// );
  /// ```
  Widget clickShowLarge({required String url, Color barrierColor = Colors.black54}) {
    return InkWell(
      onTap: () {
        FileUtils.openFile(url);
      },
      child: this,
    );
  }

  /// 点击后全屏显示图片
  Widget fullScreen(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => Scaffold(
              body: Center(
                child: InteractiveViewer(
                  child: this,
                ),
              ),
            ).clickOnce(() {
              Navigator.pop(context);
            }),
          ),
        );
      },
      child: this,
    );
  }
}

extension ListWidget on Iterable<Widget> {
  /// 将 [List<Widget>] 转换为 [Column]
  Widget toColumn({
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
  }) {
    return Column(
      mainAxisSize: mainAxisSize,
      crossAxisAlignment: crossAxisAlignment,
      mainAxisAlignment: mainAxisAlignment,
      children: toList(),
    );
  }

  /// 将 [List<Widget>] 转换为 [Row]
  Widget toRow({
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
  }) {
    return Row(
      mainAxisSize: mainAxisSize,
      crossAxisAlignment: crossAxisAlignment,
      mainAxisAlignment: mainAxisAlignment,
      children: toList(),
    );
  }

  // ignore: comment_references
  /// 类似 [String.join]，在每个 Widget 之间添加一个 [item]
  List<Widget> joinWith(Widget item) {
    final List<Widget> result = [];
    final list = toList();
    for (int i = 0; i < length; i++) {
      result.add(list[i]);
      if (i != length - 1) {
        result.add(item);
      }
    }
    return result;
  }
}
